package com.black.app.service.impl.store;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.black.app.domain.req.store.add.AppStoreProductsAddReq;
import com.black.app.domain.req.store.query.AppStoreProductsQueryReq;
import com.black.app.domain.resp.store.AppStoreProductsResp;
import com.black.app.domain.store.AppStoreProducts;
import com.black.app.service.store.AppStoreProductsService;
import com.black.app.mapper.store.AppStoreProductsMapper;
import com.black.common.exception.ServiceException;
import com.black.common.exception.database.PrimaryException;
import com.black.common.exception.database.RecordNotFoundException;
import com.black.common.utils.MessageUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* @author 12572
* @description 针对表【app_store_products】的数据库操作Service实现
* @createDate 2024-10-28 16:39:25
*/
@Service
public class AppStoreProductsServiceImpl extends ServiceImpl<AppStoreProductsMapper, AppStoreProducts>
    implements AppStoreProductsService{

    @Override
    public PageInfo<AppStoreProductsResp> paginationQuery(AppStoreProductsQueryReq req) {
        return PageHelper.startPage(req.getPageNum(), req.getPageSize()).doSelectPageInfo(() -> {
            this.baseMapper.paginationQuery(req);
        });
    }

    @Override
    public List<AppStoreProductsResp> findByCidAll(Integer categoryId) {
        LambdaQueryWrapper<AppStoreProducts> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AppStoreProducts::getSuperId, categoryId);
        List<AppStoreProducts> products = this.list(wrapper);
        return products.stream().map(this::convertToResp).collect(Collectors.toList());
    }

    @Override
    public AppStoreProductsResp findById(Long id) {
        if (Objects.isNull(id)){
            throw new PrimaryException();
        }
        //根据主键查询
        AppStoreProducts appStoreProducts = this.getById(id);
        if (Objects.isNull(appStoreProducts)){
            throw new RecordNotFoundException();
        }
        AppStoreProductsResp resp = new AppStoreProductsResp();
        BeanUtils.copyProperties(appStoreProducts,resp);
        return resp;
    }

    @Override
    public boolean add(AppStoreProductsAddReq req) {
        //校验产品名称是否已存在
        if (this.baseMapper.exists(new LambdaQueryWrapper<AppStoreProducts>().eq(AppStoreProducts::getProductName, req.getProductName()))){
            throw new ServiceException(MessageUtils.message("appstoreproducts.productName.exists"));
        }
        AppStoreProducts newEntity = new AppStoreProducts();
        BeanUtils.copyProperties(req, newEntity);
        boolean save = this.save(newEntity);
        return save;
    }

    @Override
    public boolean deleteById(Long[] ids) {
        if (Objects.isNull(ids) || ids.length == 0) {
            throw new PrimaryException();
        }
        for (Long id : ids) {
            AppStoreProducts appStoreProducts = this.getById(id);
            if (Objects.nonNull(appStoreProducts)) {
                this.removeById(id);
            }
        }
        return true;
    }

    private AppStoreProductsResp convertToResp(AppStoreProducts product) {
        AppStoreProductsResp resp = new AppStoreProductsResp();
        // 假设 AppStoreProducts 和 AppStoreProductsResp 有相同的字段
        resp.setProductId(product.getProductId());
        resp.setProductName(product.getProductName());
        resp.setCategoryId(product.getSuperId());
        resp.setImageUrl(product.getImageUrl());
        resp.setProductPrice(product.getProductPrice());
        resp.setDetail(product.getDetail());
        resp.setIsDiscount(product.getIsDiscount());
        // 其他字段的转换
        return resp;
    }
}

